package com.dingreading.cloud.common.util;

import org.springframework.beans.BeanWrapper;
import org.springframework.beans.BeanWrapperImpl;

import java.util.*;

/**
 * 支付相关工具
 */
public class KitUtil {

    /**
     * 获取指定长度的随机字符串
     *
     * @param length 指定长度
     * @return 随机字符串
     */
    @SuppressWarnings("SameParameterValue")
    public static String getRandomStr(int length) {
        String base = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
        Random random = new Random();
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < length; i++) {
            int number = random.nextInt(base.length());
            sb.append(base.charAt(number));
        }
        return sb.toString();
    }

    /**
     * 获取指定长度的随机字符串，没有大写字母和0
     *
     * @param length 指定长度
     * @return 随机字符串
     */
    @SuppressWarnings("SameParameterValue")
    public static String getRandomPwdStr(int length) {
        String base = "abcdefghijkmnpqrstuvwxyz23456789";
        Random random = new Random();
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < length; i++) {
            int number = random.nextInt(base.length());
            sb.append(base.charAt(number));
        }
        return sb.toString();
    }

    /**
     * 获取指定长度的随机数字字符串
     *
     * @param length 指定长度
     * @return 随机字符串
     */
    @SuppressWarnings("SameParameterValue")
    public static String getRandomNum(int length) {
        String base = "0123456789";
        Random random = new Random();
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < length; i++) {
            int number = random.nextInt(base.length());
            sb.append(base.charAt(number));
        }
        return sb.toString();
    }

    /**
     * 获取指定长度的随机字符串，没有大写字母和0
     *
     * @param length 指定长度
     * @return 随机字符串
     */
    @SuppressWarnings("SameParameterValue")
    public static String getUpperRandomStr(int length) {
        String base = "ABCDEFGHJKLMNPQRSTUVWXYZ23456789";
        Random random = new Random();
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < length; i++) {
            int number = random.nextInt(base.length());
            sb.append(base.charAt(number));
        }
        return sb.toString();
    }

    /**
     * 获取收支记录流水号
     * 18
     *
     * @return 随机字符串
     */
    public static String getBalanceCode() {
        return EnumType.CodeCategory.PAY.getType() + DateUtil.getNowDateminStr() + getRandomNum(4);
    }

    /**
     * 获取商品订单流水号
     * 18
     *
     * @return 随机字符串
     */
    public static String getGoodsCode() {
        return EnumType.CodeCategory.TOB.getType() + DateUtil.getNowDateminStr() + getRandomNum(4);
    }

    /**
     * 获取推广活动流水号
     * 18
     *
     * @return 随机字符串
     */
    public static String getMemberOrderCode() {
        return EnumType.CodeCategory.TOC.getType() + DateUtil.getNowDateminStr() + getRandomNum(4);
    }

    /**
     * 获取采购单流水号
     * 18
     *
     * @return 随机字符串
     */
    public static String getPurchaseCode() {
        return EnumType.CodeCategory.CGDD.getType() + DateUtil.getNowDateminStr();
    }

    /**
     * 获取入库流水号
     * 18
     *
     * @return 随机字符串
     */
    public static String getInCode() {
        return EnumType.CodeCategory.RK.getType() + DateUtil.getNowDateminStr() + KitUtil.getRandomStr(4);
    }

    /**
     * 获取会员服务订单编号
     * 18
     *
     * @return 随机字符串
     */
    public static String getMemberServiceCode() {
        return EnumType.CodeCategory.CM.getType() + DateUtil.getNowDateminStr() + KitUtil.getRandomNum(4);
    }

    /**
     * 获取会员服务订单编号
     * 18
     *
     * @return 随机字符串
     */
    public static String getCmorderApplyCode() {
        return EnumType.CodeCategory.AC.getType() + DateUtil.getNowDateminStr() + KitUtil.getRandomNum(4);
    }

    /**
     * 获取暑期老生专场订单编号
     * 18
     *
     * @return 随机字符串
     */
    public static String getSummerStudentCode() {
        return EnumType.CodeCategory.TOB.getType() + DateUtil.getNowDateminStr() + KitUtil.getRandomNum(4);
    }

    /**
     * 获取合同编号
     * 18
     *
     * @return 随机字符串
     */
    public static String getHtCode() {
        return EnumType.CodeCategory.HTBH.getType() + DateUtil.getNowDateminStr();
    }

    /**
     * 获取出库流水号
     * 18
     *
     * @return 随机字符串
     */
    public static String getOutCode() {
        return EnumType.CodeCategory.CK.getType() + DateUtil.getNowDateminStr();
    }

    /**
     * 获取南京大会门票订单号
     * 18
     *
     * @return 随机字符串
     */
    public static String getNjCode() {
        return EnumType.CodeCategory.NJ.getType() + DateUtil.getNowDateminStr() + KitUtil.getRandomNum(4);
    }

    /**
     * 名师堂体验订单号
     * 18
     *
     * @return 随机字符串
     */
    public static String getReadstarOrderCode() {
        return EnumType.CodeCategory.RD.getType() + DateUtil.getNowDateminStr() + getRandomNum(4);
    }

    /**
     * 教务系统激活码
     *
     * @return 随机字符串
     */
    public static String getActivationCode() {
        return "ACODE" + DateUtil.getNowDateminStr() + getRandomNum(4);
    }
    /**
     * 判断字符串为null返回空值
     *
     * @return 空
     */
    public static String checkStr(String str) {
        return str == null ? "" : str;
    }

    /**
     * 判断数字为null返回0
     *
     * @return 0
     */
    public static Integer checkNum(Integer str) {
        return str == null ? 0 : str;
    }

    /**
     * 获取复制对象,排除null值
     *
     * @return 随机字符串
     */
    public static String[] getNullPropertyNames(Object source) {
        final BeanWrapper src = new BeanWrapperImpl(source);
        java.beans.PropertyDescriptor[] pds = src.getPropertyDescriptors();

        Set<String> emptyNames = new HashSet<>();
        for (java.beans.PropertyDescriptor pd : pds) {
            Object srcValue = src.getPropertyValue(pd.getName());
            if (srcValue == null) {
                emptyNames.add(pd.getName());
            }
        }
        String[] result = new String[emptyNames.size()];
        return emptyNames.toArray(result);
    }

    /**
     * 将int类型的数字转换成中文字符的数字，比如123==>一百二十三
     *
     * @param src 数值
     * @return obj
     */
    public static String int2chineseNum(int src) {
        final String[] num = {"零", "一", "二", "三", "四", "五", "六", "七", "八", "九"};
        final String[] unit = {"", "十", "百", "千", "万", "十", "百", "千", "亿", "十", "百", "千"};
        String dst = "";
        int count = 0;
        while (src > 0) {
            dst = (num[src % 10] + unit[count]) + dst;
            src = src / 10;
            count++;
        }
        return dst.replaceAll("零[千百十]", "零").replaceAll("零+万", "万")
                .replaceAll("零+亿", "亿").replaceAll("亿万", "亿零")
                .replaceAll("零+", "零").replaceAll("零$", "");
    }

    /**
     * 获取UUID
     *
     * @return 32位随机字符串
     */
    public static String getUuid() {
        String uuid = UUID.randomUUID().toString();
        uuid = uuid.replace("-", "");
        return uuid;
    }

    /**
     * 获取UUID
     *
     * @return 32位随机字符串
     */
    public static List<String> getUuid(int size) {
        String uuid = UUID.randomUUID().toString();
        uuid = uuid.replace("-", "");
        List<String> list = new ArrayList<>();
        for (int i = 0; i < size; i++) {
            if (i == 0) {
                list.add(uuid);
            } else {
                list.add(uuid + (i + 1));
            }
        }
        return list;
    }

}